#!/bin/bash


total=0
converted=0

while read line
do
  if test `echo $line | awk -F', ' '{print NF}'` -ne 13
  then
    echo $line
    continue
  fi
  let total++

  modelidvc=`echo $line | awk -F', ' '{print $1}'`
  data=`awk '{ if($1=="'$modelidvc'") { print $2, $3 } }' table.txt`
  
  modelid=`echo $data | awk '{print $1}'`
  if test "$modelid" = ""; 
  then 
    echo "$modelidvc: not found!" > /dev/stderr
    continue
  fi
  modelname=`echo $data | awk '{print $2}'`

  interior=`echo $line | awk -F', ' '{print $3}'`

  posx=`echo $line | awk -F', ' '{print $4}'`
  posy=`echo $line | awk -F', ' '{print $5}'`
  posz=`echo $line | awk -F', ' '{print $6}'`

  rotx=`echo $line | awk -F', ' '{print $10}'`
  roty=`echo $line | awk -F', ' '{print $11}'`
  rotz=`echo $line | awk -F', ' '{print $12}'`
  rotw=`echo $line | awk -F', ' '{print $13}' | tr -d '\n\r\f\v\t'`


  posx=`echo "$posx + 2000.00" | bc -l`
  posy=`echo "$posy -  950.00" | bc -l`
  posz=`echo "$posz -    5.50" | bc -l`

  echo "$modelid, $modelname, $interior, $posx, $posy, $posz, $rotx, $roty, $rotz, $rotw, -1"
  let converted++
done

echo "converted $converted/$total objects" > /dev/stderr
